package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.SnowflakeIdUtils;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AnpingUser;
import com.ruoyi.system.service.IAnpingUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 安评用户信息Controller
 * 
 * @author ruoyi
 * @date 2021-04-22
 */
@Api(value="安评用户信息",description = "安评用户信息")
@RestController
@RequestMapping("/system/anpinguser")
public class AnpingUserController extends BaseController
{
    @Autowired
    private IAnpingUserService anpingUserService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询安评用户信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(AnpingUser anpingUser)
    {
        startPage();
        List<AnpingUser> list = anpingUserService.selectAnpingUserList(anpingUser);
        return getDataTable(list);
    }

    /**
     * 导出安评用户信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:user:export')")
    @Log(title = "安评用户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AnpingUser anpingUser)
    {
        List<AnpingUser> list = anpingUserService.selectAnpingUserList(anpingUser);
        ExcelUtil<AnpingUser> util = new ExcelUtil<AnpingUser>(AnpingUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取安评用户信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") String userId)
    {
        return AjaxResult.success(anpingUserService.selectAnpingUserById(userId));
    }

    /**
     * 新增安评用户信息
     */
    //@PreAuthorize("@ss.hasPermi('system:user:add')")
    @ApiOperation(value="新增安评用户信息")
    @Log(title = "安评用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(name="用户对象",value="传入json格式",required=true)@RequestBody AnpingUser anpingUser)
    {
        //判断用户是否已注册过
        if(StringUtils.isNotEmpty(anpingUser.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(anpingUser.getPhonenumber()))){
            return AjaxResult.error("注册用户'" + anpingUser.getPhonenumber() + "'失败，手机号码已存在,请直接登录");
        }
        //个人用户注册
        SysUser user = new SysUser();
        Long userId =  new SnowflakeIdUtils(0, 0).nextId();
        user.setUserId(userId);
        user.setDeptId(105L);
        user.setUserName(anpingUser.getUserName());
        user.setNickName(anpingUser.getUserName());
        user.setPhonenumber(anpingUser.getPhonenumber());
        user.setSex(anpingUser.getSex());
        user.setPassword(SecurityUtils.encryptPassword(anpingUser.getPassword()));
        userService.insertUser(user);
        anpingUser.setUserId(String.valueOf(new SnowflakeIdUtils(0, 0).nextId()));
        return toAjax(anpingUserService.insertAnpingUser(anpingUser));
    }

    /**
     * 修改安评用户信息
     */
    //@PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "安评用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AnpingUser anpingUser)
    {
        return toAjax(anpingUserService.updateAnpingUser(anpingUser));
    }

    /**
     * 删除安评用户信息
     */
    //@PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "安评用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable String[] userIds)
    {
        return toAjax(anpingUserService.deleteAnpingUserByIds(userIds));
    }
}
